package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserInformation;
import cn.wolfcode.trip.base.mapper.InformationMapper;
import cn.wolfcode.trip.base.mapper.UserInformationMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.InformationQuery;
import cn.wolfcode.trip.base.query.MessageQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IInformationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.PAData;

import javax.sound.sampled.Line;
import java.util.List;
import java.util.Map;

/**
 * InformationServiceImpl class
 *
 * @author lishuanwang
 * @date 2019/5/30
 */
@Service
public class InformationServiceImpl implements IInformationService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;

    /**
     * 插入一条系统消息,保存用户和消息之间的关系
     *
     * @param information: 消息记录
     */
    @Override
    public void save(Information information) {
        informationMapper.insert(information);
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            UserInformation userMessage = new UserInformation();
            userMessage.setInformation(information);
            userMessage.setUser(user);
            userInformationMapper.insert(userMessage);
        }
    }

    @Override
    public void saveUserInformation(Information information, User user) {
        informationMapper.insert(information);
        UserInformation userMessage = new UserInformation();
        userMessage.setInformation(information);
        userMessage.setUser(user);
        userInformationMapper.insert(userMessage);
    }


    @Override
    public PageInfo<Information> listAll(InformationQuery qo) {
        qo.setOrderBy("i.create_time desc");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Information> list = informationMapper.selectAll(qo);
        return new PageInfo<>(list);
    }
    @Override
    public PageInfo<Map> listForInfo(InformationQuery qo) {
        qo.setPageSize(10);
        qo.setOrderBy("i.create_time desc");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Map> list = informationMapper.selectForInfo(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void batchUpdateInfoRead(InformationQuery qo) {
        userInformationMapper.updateReadInfos(qo);
    }

    @Override
    public int getUnReadInfoNums(Long userId, Integer type) {
        return userInformationMapper.getUnReadInfoNums(userId, type);
    }

    @Override
    public PageInfo<Map> listInfoByUserId(MessageQuery qo) {
        qo.setOrderBy("i.create_time desc");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Map> list = informationMapper.selectByUserId(qo);
        return new PageInfo<>(list);
    }

    @Override
    public int readInfo(Long userId, Long inforId) {
        UserInformation userInfo = userInformationMapper.getUserInfo(userId, inforId);
        if (userInfo != null && !userInfo.getIsRead()) {
            userInformationMapper.updateRead(userId, inforId);
            return 1;
        }
        return 0;
    }

    @Override
    public Information getOneInformation(Long userId, Long inforId) {
        return informationMapper.selectByPrimaryKey(userId, inforId);
    }
}
